//
//  GLRotateAnimation.m
//  LMPhotoVideoSDK
//
//  Created by Aimar on 16/7/20.
//  Copyright © 2016年 魁钺会. All rights reserved.
//

#import "GLRotateAnimation.h"

@interface GLRotateAnimation ()

@property (nonatomic, assign)float      from;
@property (nonatomic, assign)float      to;
@property (nonatomic, assign)vec2      pivot;

@end

@implementation GLRotateAnimation

- (id)initWithFrom:(float)from to:(float)to {
    return [self initWithFrom:from to:to pivot:vec2(0, 0)];
}

- (id)initWithFrom:(float)from to:(float)to pivot:(vec2)pivot {
    self = [super init];
    if (self) {
        self.from = from;
        self.to = to;
        self.pivot = pivot;
    }
    
    return self;
}

- (void)applyTransformation:(float)interpolatedTime outTransform:(Matrix4<float> *)transform {
    float degrees = self.from + ((self.to - self.from) * interpolatedTime);
    
    if (self.pivot.x == 0.0f && self.pivot.y == 0.0f) {
        (*transform) = mat4::RotateZ(degrees) * (*transform);
    } else {
        Matrix4<float> m;
        m *= mat4::Translate(-self.pivot.x, self.pivot.y, 0);
        m *= mat4::RotateZ(degrees);
        m *= mat4::Translate(self.pivot.x, -self.pivot.y, 0);
        (*transform) = m * (*transform);
    }
}

@end
